/**
 * 依赖：org.springframework.boot:spring-boot-starter-amqp
 * Producer：
 * 1、application.yml文件配置RabbitMQ相关信息：主要是Connection信息
 * 2、编写配置类，用于创建交换机和队列，并进行绑定：主要是Exchange和Queue信息
 * 3、注入RabbitTemplate对象，通过RabbitTemplate对象发送消息到交换机，rabbitTemplate.convertAndSend
 * <p>
 * Consumer：
 * 1、application.yml文件配置RabbitMQ相关信息：主要是Connection信息
 * 2、创建消息处理类，用于接收队列中的消息并进行处理，@RabbitListener(queues = "xxx")
 */
package com.example.rabbitmqspringbootdemo;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableRabbit   //启用@RabbitListener，使其"bindings = @QueueBinding"参数中定义的交换器、队列及其绑定生效
public class RabbitMqSpringBootDemoApplication {

    @Autowired
    private static RabbitAdmin rabbitAdmin;

    public static void main(String[] args) {
        SpringApplication.run(RabbitMqSpringBootDemoApplication.class, args);

        //使用代码定义Exchange、Queue、Binding
        rabbitAdmin.declareQueue(new Queue("test.direct.queue"));
        rabbitAdmin.declareExchange(new DirectExchange("test.direct"));
        rabbitAdmin.declareBinding(new Binding("test.direct.queue", Binding.DestinationType.QUEUE, "test.direct", "mq.direct", null));
        rabbitAdmin.purgeQueue("test.direct.queue", false);  //清空队列消息
    }

}
